/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tetrix;

import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

/**
 *
 * @author juan.osorio
 */
public class Figura {
    
    private final int tipo_figura;
    private int WIDTH;
    private int HEIGHT;
    private int ROTACION;
    private int[][] shape;
    private Color color;
    
    public Figura(int tipo){
        this.tipo_figura = tipo;
        this.generarFigura();
        this.ROTACION = 0;
    }
    
    public final void generarFigura(){
        switch(tipo_figura){
            case 0:
                shape = new int[][]{{1,1,1,1}};
                WIDTH = 4;
                HEIGHT = 1;
                this.color = new Color(0xFF, 0xFF, 0x00);
                break;
            case 1:
                shape = new int[][]
                {{0,2,0},
                 {2,2,2}};
                WIDTH = 3;
                HEIGHT = 2;
                this.color = new Color(0xFF, 0xBF, 0x00);
                break;
            case 2:
                shape = new int[][]
                {{0,3,3},
                 {3,3,0}};
                WIDTH = 3;
                HEIGHT = 2;
                this.color = new Color(0x00, 0xFF, 0xBF);
                break;
            case 3:
                shape = new int[][]
                {{4,4,0},
                 {0,4,4}};
                WIDTH = 3;
                HEIGHT = 2;
                this.color = new Color(0x36, 0xD9, 0x00);
                break;
            case 4:
                shape = new int[][]
                {{5,5},
                 {5,5}};
                WIDTH = 2;
                HEIGHT = 2;
                this.color = new Color(0xB2, 0x00, 0x00);
                break;
            case 5:
                shape = new int[][]
                {{6,6,6},
                 {6,0,0}};
                WIDTH = 3;
                HEIGHT = 2;
                this.color = new Color(0xFF, 0x00, 0x00);
                break;
            case 6:
                shape = new int[][]
                {{7,7,7},
                 {0,0,7}};
                WIDTH = 3;
                HEIGHT = 2;
                this.color = new Color(0x85, 0x00, 0xB2);
                break;
        }
    }
    
    public void imprimir(){
        for(int i=0; i<this.HEIGHT; i++){
            System.out.print("[");
            for(int j=0; j<this.WIDTH; j++){
                System.out.print(this.shape[i][j]);
                if(j != this.WIDTH-1)
                    System.out.print(",");
            }
            System.out.print("]\n");
        }
    }
    
    public void rotar(){
        int[][] aux = null;
        int widthAux = -1;
        switch(this.ROTACION){
            case 0: case 2:
                aux = new int[this.WIDTH][this.HEIGHT];
                for(int i=0; i<this.HEIGHT; i++){
                    for(int j=0; j<this.WIDTH; j++){
                        aux[j][i] = shape[this.HEIGHT-(i+1)][j];
                    }
                }
                this.ROTACION = (this.ROTACION == 0) ? 1 : 3;
                break;
            case 1: case 3:
                aux = new int[this.WIDTH][this.HEIGHT];
                for(int i=0; i<this.HEIGHT; i++){
                    for(int j=0; j<this.WIDTH; j++){
                        aux[j][i] = shape[this.HEIGHT-(i+1)][j];
                    }
                }
                this.ROTACION = (this.ROTACION == 1) ? 2 : 0;
                if(this.ROTACION == 1){
                    this.ROTACION = 2;
                }else{
                    this.ROTACION = 0;
                }
                break;
        }
        widthAux = this.WIDTH;
        this.WIDTH = this.HEIGHT;
        this.HEIGHT = widthAux;
        this.shape = aux;
    }
    
    public JLabel[][] imprimirFigura(JPanel panel){
        int ancho = (panel.getWidth()/2)-(this.WIDTH*20)/2;
        int alto = (panel.getHeight()/2)-(this.HEIGHT*20)/2;
        JLabel[][] table = new JLabel[this.HEIGHT][this.WIDTH];
        Border border = LineBorder.createGrayLineBorder();
        for(int i=0; i<this.HEIGHT; i++){
            for(int j=0; j<this.WIDTH; j++){
                if(shape[i][j]!=0){
                    JLabel etiqueta = new JLabel();
                    etiqueta.setBounds(ancho+j*20, alto+i*20, 20, 20);
                    etiqueta.setBorder(border);
                    etiqueta.setBackground(color);
                    etiqueta.setOpaque(true);
                    etiqueta.setVisible(true);
                    table[i][j] = etiqueta;
                }else{
                    table[i][j] = null;
                }
            }
        }
        return table;
    }
    
}
